package com.woniuxy.car.boss.app.util;

import com.woniuxy.car.common.exceptions.CarException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class GetToken {
    public static Integer getUserToken() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String h1 = request.getHeader("userId");
        String h2 = request.getHeader("userType");
        if(!h2.equals("user")) throw new CarException(403,"用户未登录");
        Integer userId=Integer.valueOf(h1);
        return userId;
    }

    public static Integer getStoreToken() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String h1 = request.getHeader("userId");
        String h2 = request.getHeader("userType");
        if(!h2.equals("store")) throw new CarException(403,"店家未登录");
        Integer storeId=Integer.valueOf(h1);
        return storeId;
    }

    public static Integer getBossToken() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String h1 = request.getHeader("userId");
        String h2 = request.getHeader("userType");
        if(!h2.equals("boss")) throw new CarException(403,"店家未登录");
        Integer platformId=Integer.valueOf(h1);
        return platformId;
    }
}
